package com.practice;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class Test {
	public static void main(String[] args) {
		test2();
		//resize("e:\\res.jpg",100,50,false);
	}
	
	 /**
     * 添加图片水印
     * @param targetImg 目标图片路径，如：C://myPictrue//1.jpg
     * @param waterImg  水印图片路径，如：C://myPictrue//logo.png
     * @param x 水印图片距离目标图片左侧的偏移量，如果x<0, 则在正中间
     * @param y 水印图片距离目标图片上侧的偏移量，如果y<0, 则在正中间
     * @param alpha 透明度(0.0 -- 1.0, 0.0为完全透明，1.0为完全不透明)
*/
	public static void test2(){
		File merFile = new File("e:\\2.jpg");//商品图片
		File labFile = new File("e:\\1.png");//标签图片
		File resFile = new File("e:\\res.jpg");//结果图片
		try {
			BufferedImage merImg = ImageIO.read(merFile);//商品图片
			int merWidth = merImg.getWidth();
			int merHeight = merImg.getHeight();
			BufferedImage labImg = ImageIO.read(labFile);//标签图片
			int labWidth = labImg.getWidth();
			int labHeight = labImg.getHeight();
			BufferedImage resImg = new BufferedImage(merWidth, merHeight, BufferedImage.TYPE_INT_RGB);//结果图片
			
			Graphics2D g2D = resImg.createGraphics();//获取画笔
			g2D.drawImage(merImg, 0, 0, merWidth, merHeight, null);
			g2D.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 1));//alpha 透明度(0.0 -- 1.0, 0.0为完全透明，1.0为完全不透明)
			/*int x = 20,y=10;
			int widthDiff = merWidth - labWidth;
			int heightDiff = merHeight - labHeight;
			if (x < 0) {
				x = widthDiff / 2;
			} else if (x > widthDiff) {
				x = widthDiff;
			}
			if (y < 0) {
				y = heightDiff / 2;
			} else if (y > heightDiff) {
				y = heightDiff;
			}*/
			g2D.drawImage(labImg, 0, 0, labWidth, labHeight, null); // 水印文件结束
			g2D.dispose();//销毁画笔
			
			ImageIO.write(resImg, "jpg", resFile);
			
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 图片缩放
	 * 
	 * @param filePath
	 *            图片路径
	 * @param height
	 *            高度
	 * @param width
	 *            宽度
	 * @param bb
	 *            比例不对时是否需要补白
	 */
	public static void resize(String filePath, int height, int width, boolean bb) {
		try {
			double ratio = 0; // 缩放比例
			File f = new File(filePath);
			BufferedImage bi = ImageIO.read(f);
			Image itemp = bi.getScaledInstance(width, height,BufferedImage.SCALE_SMOOTH);
			// 计算比例
			if ((bi.getHeight() > height) || (bi.getWidth() > width)) {
				if (bi.getHeight() > bi.getWidth()) {
					ratio = (new Integer(height)).doubleValue() / bi.getHeight();
				} else {
					ratio = (new Integer(width)).doubleValue() / bi.getWidth();
				}
				AffineTransformOp op = new AffineTransformOp(AffineTransform.getScaleInstance(ratio, ratio), null);
				itemp = op.filter(bi, null);
			}
			if (bb) {
				BufferedImage image = new BufferedImage(width, height,
						BufferedImage.TYPE_INT_RGB);
				Graphics2D g = image.createGraphics();
				g.setColor(Color.white);
				g.fillRect(0, 0, width, height);
				if (width == itemp.getWidth(null))
					g.drawImage(itemp, 0, (height - itemp.getHeight(null)) / 2,
							itemp.getWidth(null), itemp.getHeight(null),
							Color.white, null);
				else
					g.drawImage(itemp, (width - itemp.getWidth(null)) / 2, 0,
							itemp.getWidth(null), itemp.getHeight(null),
							Color.white, null);
				g.dispose();
				itemp = image;
			}
			ImageIO.write((BufferedImage) itemp, "jpg", f);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
